package com.config;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linyukun
 * @date 2022/7/6 17:30
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Value("${sys.swagger.hidden:false}")
    boolean swaggerHidden;
    @Value("${sys.swagger.version:v0.0.0-0}")
    String swaggerVersion;
    @Value("${sys.swagger.apiversion:}")
    String swaggerApiVersion;
    @Value("${spring.profiles.active}")
    String active;



    @Bean
    public Docket createRestApi() {
        Class clz = Api.class;
        if(swaggerHidden)   //隐藏接口
            clz = SwaggerConfig.class;
        ParameterBuilder aParameterBuilder = new ParameterBuilder();
        aParameterBuilder.name("_API_VERSION")
                .description("接口版本号")
                .parameterType("query")
                .defaultValue(swaggerApiVersion).required(false).modelRef(new ModelRef("string")).build();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("user_token")
                .description("用户token（仅登录时不需要）")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        ParameterBuilder uidPar = new ParameterBuilder();
        uidPar.name("uid")
                .description("用户id(仅登录时不需要)")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();

        List<Parameter> aParameters = new ArrayList<Parameter>();
        aParameters.add(aParameterBuilder.build());
        aParameters.add(tokenPar.build());
        aParameters.add(uidPar.build());

        Predicate<RequestHandler> invetigateSelector = RequestHandlerSelectors.basePackage("com.controller.investigate.api");
        Predicate<RequestHandler> invetigateSelector1 = RequestHandlerSelectors.basePackage("com.controller.forecast");
        Predicate<RequestHandler> sjgjSelector2 = RequestHandlerSelectors.basePackage("com.controller.sjgj");//数据共建小程序
        Predicate<RequestHandler> finalPredicate = null ;
        //finalPredicate = Predicates.alwaysTrue();
        if( "prod".equals( active ) ){
            finalPredicate = Predicates.alwaysFalse();
        }else{
            finalPredicate = Predicates.or( invetigateSelector,invetigateSelector1,sjgjSelector2 );
        }
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()).useDefaultResponseMessages(false).globalOperationParameters(aParameters)
                .select()
                .apis( finalPredicate )
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("图书管理系统")
                .description("此API由林钰坤开发维护，请严格遵守使用规则。")
                //.contact("WinshangData")
                //.contact(new Contact("WinshangData", "http://www.winshangdata.com", "wenbanhui@winshang.com"))
                .version(swaggerVersion)
                .build();
    }
}
